<template>
	<div class="system-edit-role-container">
		<el-dialog :title="(pd === 0 ? '添加' : '修改') + '会员'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="会员用户ID" prop="id">
							<el-input v-model="formData.id" placeholder="请输入会员ID" clearable></el-input>
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="会员用户名" prop="username">
							<el-input v-model="formData.username" placeholder="请输入会员账号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="密码" prop="password">
							<el-input v-model="formData.password" :disabled="pd === 0?false:true" :placeholder="pd === 0?'请输入取款密码':'******'"  clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="真实姓名" prop="realname">
							<el-input v-model="formData.realname" :disabled="pd === 0?false:true" :placeholder="pd === 0?'请输入真实姓名':'******'" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="等级(1-10)" prop="level">
							<el-tree-select v-model="formData.level" :data="data" filterable />
							<!-- <el-input v-model="formData.level" placeholder="请输入等级" clearable></el-input> -->
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="取款密码" prop="safePassword">
							<el-input v-model="formData.safePassword" placeholder="请输入密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否代理" prop="isAgent">
							<el-switch
								v-model="formData.isAgent"
								:active-value="1"
								:inactive-value="0"
								inline-prompt
								active-text="启"
								inactive-text="禁"
							></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="上级代理" prop="parentAgentId">
							<el-select
								v-model="formData.parentAgentId"
								filterable
								remote
								reserve-keyword  
								placeholder="请输入上级代理人昵称"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item
							label="手机号
"
							prop="mobile"
						>
							<el-input
								v-model="formData.mobile"
								placeholder="请输入手机号
"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱" prop="email">
							<el-input v-model="formData.email" placeholder="请输入邮箱" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="来源">
							<el-input v-model="formData.source" type="textarea" placeholder="请输入来源" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default" :loading="loading">{{ pd === 0 ? '新 增' : '修 改' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref, getCurrentInstance, unref, onMounted } from 'vue';
import { apiMemberListaddRole, editRole, getRole, getRoleParams, apiMemberList } from '/@/api/MemberManagement/MemberList';
import { ElMessage } from 'element-plus';
import { refreshBackEndControlRoutes } from '/@/router/backEnd';
// 定义接口来定义对象的类型
interface MenuDataTree {
	id: number;
	pid: number;
	title: string;
	children?: MenuDataTree[];
}

interface DialogRow {
	id:Number,
	username: string; //会员账户
	password: string; //会员密码
	realname: string; //真是姓名
	level: number; //等级
	// sourceData: Array<sourceDataff>;
	safePassword: number; //取款密码
	isAgent: number; //是否代理
	parentAgentId: string; //代理ID
	// isdevelop: number; //是否发展代理
	// profit: number; //余额
	// coin: number; //积分
	mobile: number; //手机号
	email: number; //邮箱
	// status: number; //状态
	source: string; //来源
}
interface arr {
	sourceData: Array<sourceDataff>;
}
interface sourceDataff {
	value: string;
	label: string;
}
interface RoleState {
	loading: boolean;
	isShowDialog: boolean;
	formData: DialogRow;
	menuData: arr;
	menuExpand: boolean;
	menuNodeAll: boolean;
	menuCheckStrictly: boolean;
	menuProps: {
		children: string;
		label: string;
	};
	rules: object;
}
interface ListItem {
	value: string;
	label: string;
}
export default defineComponent({
	name: 'systemEditRole',
	props: {
		// 定义message为字符串类型
		pd: Number as () => number,
	},

	setup(props, { emit }) {
		const value = ref();
		const { proxy } = getCurrentInstance() as any;
		const formRef = ref<HTMLElement | null>(null);
		const menuRef = ref();

		const state = reactive<RoleState>({
			loading: false,
			isShowDialog: false,
			menuData: {
				sourceData: [
					{ value: '1', label: '一级' },
					{ value: '2', label: '二级' },
					{ value: '3', label: '三级' },
					{ value: '4', label: '四级' },
					{ value: '5', label: '五级' },
					{ value: '6', label: '六级' },
					{ value: '7', label: '七级' },
					{ value: '8', label: '八级' },
					{ value: '9', label: '九级' },
					{ value: '10', label: '十级' },
				],
			}, 
			formData: {
				id:0,
				username: '', //会员账户
				password: '', //会员密码
				realname: '', //真是姓名
				level: NaN, //等级

				safePassword: NaN, //取款密码
				isAgent: 1, //是否代理
				parentAgentId: '', //代理ID
				// isdevelop: NaN, //是否发展代理
				// profit: NaN, //余额
				// coin: NaN, //积分
				mobile: NaN, //手机号
				email: NaN, //邮箱
				source: '', //来源
			},
			// 表单校验
			rules: {
				name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
			},

			menuExpand: false,
			menuNodeAll: false,
			menuCheckStrictly: false,
			menuProps: {
				children: 'children',
				label: 'title',
			},
		});
		const data = ref(state.menuData.sourceData);
		const options = ref<ListItem[]>([]);
		const loading = ref(false);
		const remoteMethod = (query: string) => {
			if (query) {
				apiroleLista(query);
			} else {
				options.value = [];
			}
		};
		// 搜索
		const apiroleLista = (name: String) => {
			var username = { username: name };
			apiMemberList(username).then((res) => {
				const list = res.data.userList ?? [];
				list.value = list.map((item: { id: any; username: any; }) => {
					if(list.isAgent === 1){
						return { value: `${item.id}`, label: `上级昵称:${item.username}` }; 
					}
					
				});

				loading.value = true;
				setTimeout(() => {
					loading.value = false;
					options.value = list.value.filter((item: any) => {
						console.log(item)
						return item;
					});
				}, 200);
		
			});
		};
		// 打开弹窗
		const openDialog = (row:any) => {
	
			getMenuData(); 
			if (row) {
				state.formData = row;
				var data = {id: row.id}
	
				apiMemberList(data).then((res: any) => {
					console.log(res.data.userList[0].username)
					if (res.data.userList) {
						state.formData ={
				id:	res.data.userList[0].id,		
				username: res.data.userList[0].username, //会员账户
				password: '******', //会员密码
				realname: '阿哲', //真是姓名
				level: res.data.userList[0].level, //等级
				safePassword: res.data.userList[0].safePassword, //取款密码
				isAgent: res.data.userList[0].isAgent, //是否代理
				parentAgentId: '', //代理ID
				// isdevelop: NaN, //是否发展代理
				// profit: NaN, //余额
				// coin: NaN, //积分
				mobile:res.data.userList[0].mobile, //手机号
				email: res.data.userList[0].email, //邮箱
				source: res.data.userList[0].source, //来源
						}
				
						// state.formData = res.data.userList[0];
						
						// state.formData.mobile = res.data.menuIds ?? [];
					}
				});
			}else{
				resetForm();
			}
			
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					state.loading = true;
					if (props.pd === 0) {
						//添加
						apiMemberListaddRole(state.formData)
							.then(() => {
								ElMessage.success('角色添加成功');
								closeDialog(); // 关闭弹窗
								resetMenuSession();
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					} else {
						//修改
						editRole(state.formData)
							.then(() => {
								ElMessage.success('角色修改成功');
								closeDialog(); // 关闭弹窗
								resetMenuSession();
								emit('getRoleList');
							})
							.finally(() => {
								state.loading = false;
							});
					}
				}
			});
		};
		// 获取菜单结构数据
		const getMenuData = () => {
			getRoleParams().then((res: any) => {
				state.menuData = proxy.handleTree(res.data.menu, 'id', 'pid');
			});
		};
		const resetForm = () => {
			state.menuCheckStrictly = false;
			state.menuExpand = false;
			state.menuNodeAll = false;
			state.formData = {
				// id:0,
				username: '', //会员账户
				password: '', //会员密码
				realname: '', //真是姓名
				level: 0, //等级
				safePassword: 0, //取款密码
				isAgent:1, //是否代理
				parentAgentId: '', //代理ID
				// isdevelop: NaN, //是否发展代理
				// profit: NaN, //余额
				// coin: NaN, //积分
				mobile: 0, //手机号
				email: 0, //邮箱

				source: '', //来源
			};
		};
		// 重置菜单session
		const resetMenuSession = () => {
			refreshBackEndControlRoutes();
		};
		return {
			apiroleLista,
			remoteMethod,
			onMounted,
			options,
			data,
			value,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			menuRef,
			formRef,
			resetMenuSession,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.tree-border {
	margin-top: 5px;
	border: 1px solid #e5e6e7 !important;
	border-radius: 4px;
}
.system-edit-role-container {
	.menu-data-tree {
		border: var(--el-input-border, var(--el-border-base));
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
